import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-10-16
 * Time: 22:54
 */
class Solution {
    public static boolean isValid(String s) {
        int i=0;
        Stack<Character> l=new Stack<>();
        if(l.empty()){
            l.push(s.charAt(i++));
        }
        while(i<s.length()){
            
            if(l.empty()){
                l.push(s.charAt(i));
            }else if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{'){
                l.push(s.charAt(i));
            }else if(s.charAt(i)==']'){
                if(l.peek()=='['){
                    l.pop();
                }else {
                    return false;
                }
            }else if(s.charAt(i)==')'){
                if(l.peek()=='('){
                    l.pop();
                }else {
                    return false;
                }
            }else{
                if(l.peek()=='{'){
                    l.pop();
                }else {
                    return false;
                }
            }
            i++;
        }if(l.empty()){
            return true;
        }return false;
    }
    public static void main(String[] args) {
        System.out.println(isValid("()[]{}"));
    }
}
public class Main {

}